﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading;
using WYJCore;
using WYJCore.Extension;
using WYJData.Orm;

namespace WYJWeb
{
    public class DbContext
    {
        private readonly SqlCommand SqlCommand;
        private readonly string connectionString = "Data Source=47.92.217.144;database=WYJ;uid=sa;pwd=~Zhiqing12345678";
        private SqlConnection conn;

        public DbContext()
        {
            //SqlConnection conn = null;
                //conn = new SqlConnection(connectionString);
                //打开数据库连接
                //conn.Open();

            //bool succeeded = false;
            //int totalNumberOfTimesToTry = 4;
            //int retryIntervalSeconds = 10;

            //for (int tries = 1;
            //  tries <= totalNumberOfTimesToTry;
            //  tries++)
            //{
            //    try
            //    {
            //        if (tries > 1)
            //        {
            //            Console.WriteLine
            //              ("Transient error encountered. Will begin attempt number {0} of {1} max...",
            //              tries, totalNumberOfTimesToTry
            //              );
            //            Thread.Sleep(1000 * retryIntervalSeconds);
            //            retryIntervalSeconds = Convert.ToInt32
            //              (retryIntervalSeconds * 1.5);
            //        }
            //        succeeded = true;
            //        break;
            //    }
            //    catch (SqlException sqlExc)
            //    {
            //        if (TransientErrorNumbers.Contains(sqlExc.Number))
            //        {
            //            Console.WriteLine("{0}: transient occurred.", sqlExc.Number);
            //            continue;
            //        }
            //        else
            //        {
            //            Console.WriteLine(sqlExc);
            //            succeeded = false;
            //            break;
            //        }
            //    }
            //    catch (TestSqlException sqlExc)
            //    {
            //        if (TransientErrorNumbers.Contains(sqlExc.Number))
            //        {
            //            Console.WriteLine("{0}: transient occurred. (TESTING.)", sqlExc.Number);
            //            continue;
            //        }
            //        else
            //        {
            //            Console.WriteLine(sqlExc);
            //            succeeded = false;
            //            break;
            //        }
            //    }
            //    catch (Exception Exc)
            //    {
            //        Console.WriteLine(Exc);
            //        succeeded = false;
            //        break;
            //    }
            //}

        }


        public void Update<T>(T Model)
        {
            SqlCommand.CommandText = $@"
                  UPDATE {typeof(T).Name} SET 

            ";
            var reader = SqlCommand.ExecuteReader();
        }


        public void Save<T>(T Model) where T:IModel
        {
            var type = Model.GetType();
            var cols = new Table(type).Columns;
            var values = cols.Select(col => Model.GetType().GetProperty(col.ColumnInfo.Name).GetValue(Model));
            var sql = $"INSERT INTO {type.Name} ({cols.JoinToString(" ")}) VALUES ()";

        }


    }

    [Serializable]
    internal class TestSqlException : Exception
    {
        internal TestSqlException(int testErrorNumber)
        { Number = testErrorNumber; }

        internal int Number
        { get; set; }
    }
}
